package  
{
	import flash.text.engine.BreakOpportunity;
	import flash.utils.Timer;
	import org.flixel.*;
	import flash.events.TimerEvent;
	/**
	 * ...
	 * @author d7ggj
	 */
	public class DarknessController extends FlxGroup
	{
		public var darknessWidth:int = 35;
		public var darknessHeight:int = 35;
		public var spawnInterval:int = 1750;
		public var maxBlobs:int = 20;
		public var lightCrystal:LightCrystal;
		protected var myTimer:Timer;
		
		public function DarknessController() 
		{
			super();
			myTimer = new Timer(spawnInterval);
			myTimer.addEventListener(TimerEvent.TIMER, runOnce);
			myTimer.start();
		}
		
		override public function update():void
		{
			super.update();
		}

		private function runOnce(event:TimerEvent):void 
		{
			if(countLiving() < maxBlobs)
				this.spawnBlobBorder();
		}
		
		public function spawnBlobBorder():void
		{
			var randSide:Number = Math.random() * 4;
			var spawnCood:FlxPoint = new FlxPoint(0, 0);
			
			if (randSide < 1) {
				spawnCood.x = - 18;
				spawnCood.y = Math.random() * 600;
			}else if ((randSide >= 1) && randSide < 2) {
				spawnCood.x = Math.random() * 800;
				spawnCood.y = - 18;
			}else if ((randSide >= 2) && randSide < 3) {
				spawnCood.x = 818;
				spawnCood.y = Math.random () * 600;
			}
			else {
				spawnCood.x = Math.random() * 800;
				spawnCood.y = 618;
			}
			
			spawnBlob(spawnCood);
			
			/*var p:FlxPoint = new FlxPoint();
			p.x = -darknessWidth / 2;
			p.y = -darknessHeight / 2;
			while (p.x < FlxG.width) //spawn right
			{
				spawnBlob(p);
				p.x += darknessWidth;
			}
			p.x = FlxG.width - darknessWidth / 2;
			while (p.y < FlxG.height) //spawn down
			{
				spawnBlob(p);
				p.y += darknessHeight;
			}
			p.y = FlxG.height - darknessHeight / 2;
			while (p.x > -darknessWidth / 2) //spawn left
			{
				spawnBlob(p);
				p.x -= darknessWidth;
			}
			p.x = -darknessWidth / 2;
			while (p.y > -darknessHeight / 2) //spawn down
			{
				spawnBlob(p);
				p.y -= darknessHeight;
			}*/
		}
		
		private function spawnBlob(p:FlxPoint):void
		{
			//playState.
			add(new DarknessBlob(p.x, p.y, lightCrystal));
		}
		
	}

}